package net.vistiyos.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;

import net.vistiyos.R;
import net.vistiyos.exception.GetDataSerieException;
import net.vistiyos.exception.NotSignInException;
import net.vistiyos.modelo.Serie;
import net.vistiyos.modelo.Usuario;

import org.w3c.dom.DOMException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.widget.TextView;

public class SerieActivity extends Activity {
	
	private ProgressDialog dialog;
	private Serie elegida;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
		setContentView(R.layout.serieactivity);
		Bundle bundle = getIntent().getExtras();
		String idSerie = bundle.getString("idSerie");
		ArrayList<Serie> series = Usuario.obtenerSeries(Serie.SIN_CRITERIO);
		Iterator<Serie> it = series.iterator();
		boolean fin = false;
		elegida = null;
		while(it.hasNext() && !fin){
			Serie actual = it.next();
			if(actual.getIdSerie().equalsIgnoreCase(idSerie)){
				elegida = actual;
				fin = true;
			}
		}
		try {
			new ObtenerDatos().execute().get();
			TextView nombreSerie = (TextView) findViewById(R.id.nombreserie);
			nombreSerie.setText(elegida.getTitulo());
			MyPageAdapterSerie adapter = new MyPageAdapterSerie(this,elegida);
			ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
			myPager.setAdapter(adapter);
			int temporada = bundle.getInt("temporada");
			myPager.setCurrentItem(temporada, true);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            Intent intent = new Intent(this, PanelAdministracion.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private class ObtenerDatos extends AsyncTask< Void, Void, Void>{
		
		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(SerieActivity.this, "Progreso", "Obteniendo datos...Espere por favor");
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			try {
				SerieActivity.this.elegida.obtenerDatosSerie();
			} catch	(NotSignInException e) {
				 e.printStackTrace();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (DOMException e) {
				e.printStackTrace();
			} catch (GetDataSerieException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			if (dialog != null) {
				dialog.dismiss();
			}
		}
	}

}
